package com.yumeng.common.api;

import lombok.Getter;
import org.springframework.http.HttpStatus;

/**
 * @author wxd
 * @date 2021/5/25 13:49
 */
@Getter
public enum ApiCode {

    //======================================常规======================================
    /**
     * 操作成功
     **/
    SUCCESS(2000, "操作成功", HttpStatus.OK),
    /**
     * 警告
     **/
    WARN(2500, "警告", HttpStatus.OK),
    /**
     * 失败
     **/
    FAIL(3000, "失败", HttpStatus.INTERNAL_SERVER_ERROR),
    /**
     * 没有访问权限
     **/
    FORBIDDEN(3001, "没有访问权限", HttpStatus.FORBIDDEN),
    /**
     * 请求语法异常
     **/
    BAD_REQUEST(3002, "请求语法异常", HttpStatus.BAD_REQUEST),


    //======================================认证======================================
    /**
     * 身份未认证（需重新登录）
     **/
    UNAUTHORIZED(4000, "身份未认证", HttpStatus.UNAUTHORIZED),
    /**
     * 身份认证过期
     **/
    AUTH_EXPIRED(4001, "身份认证过期", HttpStatus.UNAUTHORIZED),
    /**
     * 登录失败（需重新登录）
     **/
    LOGIN_FAIL(4010, "登录失败", HttpStatus.UNAUTHORIZED),

    //======================================密码======================================
    /**
     * 弱密码
     */
    CREDENTIALS_WEAK(4031, "弱密码", HttpStatus.OK),
    /**
     * 密码过期
     */
    CREDENTIALS_EXPIRED(4032, "密码过期", HttpStatus.OK),

    //======================================token======================================
    /**
     * token异常
     **/
    TOKEN_NORMAL_ERROR(4060, "token异常", HttpStatus.UNAUTHORIZED),
    /**
     * token已失效
     */
    TOKEN_INVALID(4061, "token已失效", HttpStatus.UNAUTHORIZED),
    /**
     * token即将失效
     */
    TOKEN_INVALID_SOON(4062, "token即将失效", HttpStatus.OK),

    /**
     * JWT刷新新token响应状态码 已刷新直接返回2000？
     **/
    //JWT_REFRESHED_TOKEN(4060, "token已刷新"),
    /**
     * JWT刷新新token响应状态码
     **/
    //JWT_INVALID_TOKEN(4061, "token已失效"),
    /**
     * 需要客户端提供refresh_token
     **/
    //JWT_NEED_REFRESH_TOKEN(4062, "请提供更新凭证"),

    //======================================错误======================================
    /**
     * 服务器异常
     **/
    ERROR(5000, "服务器异常", HttpStatus.INTERNAL_SERVER_ERROR),
    /**
     * RUNTIME异常
     **/
    RUNTIME_ERROR(5001, "RUNTIME异常", HttpStatus.INTERNAL_SERVER_ERROR),
    /**
     * SERVLET异常
     **/
    SERVLET_ERROR(5002, "SERVLET异常", HttpStatus.INTERNAL_SERVER_ERROR),
    /**
     * 未分类异常
     **/
    UNMATCH_ERROR(5098, "未分类异常", HttpStatus.INTERNAL_SERVER_ERROR),
    /**
     * 未知异常
     **/
    UNKNOWN_ERROR(5099, "未知异常", HttpStatus.INTERNAL_SERVER_ERROR),
    /**
     * 系统处理异常
     **/
    YUMENG_EXCEPTION(5101, "系统异常", HttpStatus.OK),
    /**
     * 业务处理异常
     **/
    BUSINESS_EXCEPTION(5102, "业务异常", HttpStatus.OK),
    /**
     * 认证异常
     **/
    AUTH_EXCEPTION(5201, "认证异常", HttpStatus.UNAUTHORIZED),
    /**
     * DB异常
     **/
    DB_EXCEPTION(5202, "DB异常", HttpStatus.INTERNAL_SERVER_ERROR);

    private final int code;
    private final String message;
    private final HttpStatus httpStatus;//暂时未实际使用，对团队要求较高

    ApiCode(final int code, final String message, final HttpStatus httpStatus) {
        this.code = code;
        this.message = message;
        this.httpStatus = httpStatus;
    }

    /**
     * 是否需要重新登录
     * @param apiCode
     * @return
     */
    public static boolean needReLogin(ApiCode apiCode){
        return UNAUTHORIZED.equals(apiCode)
                ||AUTH_EXPIRED.equals(apiCode)
                ||LOGIN_FAIL.equals(apiCode);
    }

    /**
     * 是否需要刷新token
     *
     * @param apiCode
     * @return
     */
    public static boolean needRefreshToken(ApiCode apiCode){
        return TOKEN_INVALID.equals(apiCode) || TOKEN_INVALID_SOON.equals(apiCode);
    }

    /**
     * 是否需要修改密码
     *
     * @param apiCode
     * @return
     */
    public static boolean needChangeCredentials(ApiCode apiCode){
        return CREDENTIALS_WEAK.equals(apiCode) || CREDENTIALS_EXPIRED.equals(apiCode);
    }

}
